﻿using System.Collections;
using System.Collections.Generic;
using Entitas;
using UnityEngine;


namespace Internal
{
    public class DirectionSystem : ReactiveSystem<GameEntity>
    {

        public DirectionSystem(Contexts contexts) : base(contexts.game)
        {

        }


        protected override void Execute(List<GameEntity> entities)
        {
            foreach (GameEntity entity in entities)
            {
                Transform view = entity.internalView.viewTrans;
                Vector3 targetPos = entity.internalMove.targetPos;
                Vector3 dir = (targetPos - view.position).normalized;
                //四元数
                //Quaternion angle = Quaternion.FromToRotation(view.up,dir);
                //view.rotation *= angle;
                //非四元数
                float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
                entity.ReplaceInternalDirection(angle);
            }
        }

        protected override bool Filter(GameEntity entity)
        {      
            return entity.hasInternalMove
                && entity.hasInternalView
                && entity.isInternalMoveComplete
                && entity.hasInternalDirection;
        }

        protected override ICollector<GameEntity> GetTrigger(IContext<GameEntity> context)
        {
            return context.CreateCollector(GameMatcher.InternalMove);
        }
    }
}

